package com.db.jdbc;

/**
 * 数据库驱动映射
 *
 * @author xin.ding
 * @date: 2020/8/3 17:21
 */
public enum DbDriverMapper {

    /**
     * derby数据库
     */
    DERBY("jdbc:derby", "org.apache.derby.jdbc.EmbeddedDriver"),
    /**
     * mysql
     */
//    MYSQL("jdbc:mysql", "com.mysql.jdbc.Driver"),
    MYSQL("jdbc:mysql", "com.mysql.cj.jdbc.Driver"),
    /**
     * oracle
     */
    ORACLE("jdbc:oracle", "oracle.jdbc.driver.OracleDriver"),
    /**
     * sql server
     */
    MSSQL("jdbc:sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver"),
    /**
     * h2
     */
    H2("jdbc:h2", "org.h2.Driver"),
    /**
     * Hive
     */
    HIVE("jdbc:hive2", "org.apache.hive.jdbc.HiveDriver");

    DbDriverMapper(String prefix, String driverClassName) {
        this.prefix = prefix;
        this.driverClassName = driverClassName;
    }

    /**
     * jdbcUrl 前缀
     */
    private String prefix;

    /**
     * 驱动类名
     */
    private String driverClassName;

    /**
     * 通过jdbcUrl 获取对应的驱动类
     *
     * @param url jdbc连接字符串
     * @return driverClass
     */
    public static String getDriverClassName(String url) {
        for (DbDriverMapper m : DbDriverMapper.values()) {
            if (url.startsWith(m.prefix)) {
                return m.driverClassName;
            }
        }
        throw new RuntimeException("未找到合适的jdbc驱动");
    }

    public String getDriverClassName() {
        return driverClassName;
    }
}
